﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Threading;
using Microsoft.DirectX.AudioVideoPlayback;
using System.ComponentModel;

namespace SoundServant
{
    public partial class Recorder
    {
        public int MusicVolume { get { return musicVolume; } set { musicVolume = value % -5000; if (musicState == MusicStates.Playing) musicPlayer.Volume = musicVolume; } }
        public string MusicCurrentlyPlaying { get { return musicFiles[musicCurrentTrack].Name; } }
        public MusicStates MusicState { get { return musicState; } }
        public int MusicPlayListCount { get { return musicFiles.Length; } }
        public string MusicSongbookName { get { return musicSongbookName; } }

        public event VoidEventHandler MusicPlaying;
        public event VoidEventHandler MusicSkipping;
        public event VoidEventHandler MusicStopping;
        public event VoidEventHandler MusicStopped;

        public void MusicPlaySkip()
        {
            if (recorderState == RecorderStates.Ready || recorderState == RecorderStates.Finished)
            {
                if (musicState == MusicStates.Ready)
                {
                    musicState = MusicStates.Playing;
                    MusicPlay();
                }
                else if (musicState == MusicStates.Playing)
                {
                    MusicSkip();
                }
            }
        }

        void ShuffleMusic()
        {
            Logs.Information("Background Music", "Shuffling music");
            int i = 0;
            Random rnd = new Random();
            for (i = 0; i < musicNumberOfTracks; i++)
            {
                int swapWith = rnd.Next(musicNumberOfTracks - 1);
                FileInfo tmp;
                tmp = musicFiles[swapWith];
                musicFiles[swapWith] = musicFiles[i];
                musicFiles[i] = tmp;
            }
        }

        void MusicPlay()
        {
            if (musicState == MusicStates.Playing)
            {
                Logs.Information("Background Music", "Playing - File: " + musicFiles[musicCurrentTrack].FullName);
                musicPlayer = new Audio(musicFiles[musicCurrentTrack].FullName);
                musicPlayer.Volume = musicVolume;
                musicPlayer.Ending += new EventHandler(MusicEnding);
                musicPlayer.Play();
                if (MusicPlaying != null)
                    MusicPlaying();
            }
        }

        void MusicEnding(object sender, EventArgs e)
        {
            MusicIncrement();
            Logs.Information("Background Music", "Song ending");
            musicPlayer.Ending -= MusicEnding;
            MusicPlay();
        }

        private void MusicIncrement()
        {
            musicCurrentTrack = (musicCurrentTrack + 1 >= musicNumberOfTracks) ? musicCurrentTrack = 0 : musicCurrentTrack + 1;
        }

        private void MusicSkip()
        {
            Logs.Information("Background Music", "Skipping song");
            musicState = MusicStates.Skiping;
            musicFader.RunWorkerAsync();
            if (MusicSkipping != null) MusicSkipping();
        }
        void MusicFader(object sender, DoWorkEventArgs e)
        {
            for (int i = musicPlayer.Volume; i > -4000; i--)
            {
                musicPlayer.Volume--;
                Thread.Sleep(1);
            }
            musicPlayer.Stop();
        }

        void MusicFadeFinished(object sender, RunWorkerCompletedEventArgs e)
        {
            musicPlayer.Dispose();
            musicPlayer = null;

            if (musicState == MusicStates.Skiping)
            {
                musicState = MusicStates.Playing;
                MusicIncrement();
                MusicPlay();
            }
            else if (musicState == MusicStates.Stopping)
            {
                musicState = MusicStates.Ready;
                MusicStopped();
                if (recorderState == RecorderStates.Starting)
                { Record(); }
            }
        }

        public void MusicStop()
        {
            if (musicState == MusicStates.Playing || musicState == MusicStates.Skiping)
            {
                Logs.Information("Background Music", "Stopping music");
                musicState = MusicStates.Stopping;
                if (!musicFader.IsBusy) musicFader.RunWorkerAsync();
                MusicStopping();
            }
        }
    }

    public enum BgMusicStatus
    {
        Ready,
        Playing,
        Skiping,
        Stopping,
        Locking,
        Locked
    }

}
